//
//  ViewController.m
//  FaceLock
//
//  Created by TTgg on 12-9-11.
//  Copyright (c) 2012年 com.ptc. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
@interface ViewController ()
static UIImage *shrinkImage (UIImage *original, CGSize size);
- (void)updateDisplay;
- (void)getMediaFromSource: (UIImagePickerControllerSourceType)sourceType;
@end

@implementation ViewController
@synthesize imageView;
@synthesize takePictureButton;
@synthesize moviePlyerController;
@synthesize movieURL;
@synthesize image;
@synthesize lastChoseMediaType;
@synthesize imageFrame;
@synthesize ap;
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        takePictureButton.hidden = YES;
    }
    imageFrame = imageView.frame;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self updateDisplay];
}

- (void)viewDidUnload
{
    [self setImageView:nil];
    [self setTakePictureButton:nil];
    [super viewDidUnload];
    [self setMoviePlyerController:nil];
    // Release any retained subviews of the main view.
}
- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


- (void)dealloc {
    [imageView release];
    [takePictureButton release];
    [super dealloc];
}
- (IBAction)shootPictureOrVideo:(id)sender {
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
    
}

- (IBAction)selectExistingPictureOrVideo:(id)sender {
    [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.lastChoseMediaType = [info objectForKey:UIImagePickerControllerMediaType];
//    AppDelegate *ap = [[AppDelegate alloc]init];
//    AppDelegate *del=(AppDelegate *)[[UIApplication sharedApplication]delegate];
//    del.imageName = lastChoseMediaType;
    ap = [[AppDelegate alloc]init];
    
    if ([lastChoseMediaType isEqual:(NSString *)kUTTypeImage]) {
        UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
        UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
        self.image = shrunkenImage;
    } else if ([lastChoseMediaType isEqual:(NSString *)kUTTypeMovie]) {
        self.movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
    }
    [picker dismissModalViewControllerAnimated:YES];
    [ap faceDetector:lastChoseMediaType];
    
//    [del release];
}
static UIImage *shrinkImage (UIImage *original, CGSize size)
{
    CGFloat scale = [UIScreen mainScreen].scale;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef conttext = CGBitmapContextCreate(NULL, size.width * scale, size.height *scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(conttext, CGRectMake(0, 0, size.width * scale, size.height * scale), original.CGImage);
    CGImageRef shrunken = CGBitmapContextCreateImage(conttext);
    UIImage *final = [UIImage imageWithCGImage:shrunken];
    
    CGContextRelease(conttext);
    CGImageRelease(shrunken);
    return final;
}
-(void)updateDisplay
{
    if ([lastChoseMediaType isEqual:(NSString *)kUTTypeImage]) {
        imageView.image = image;
        imageView.hidden = NO;
        moviePlyerController.view.hidden = YES;
    }
    else if ([lastChoseMediaType isEqual:(NSString *)kUTTypeMovie])
    {
        [self.moviePlyerController.view removeFromSuperview];
        self.moviePlyerController = [[MPMoviePlayerController alloc]initWithContentURL:movieURL];
        moviePlyerController.view.frame = imageFrame;
        moviePlyerController.view.clipsToBounds = YES;
        [self.view addSubview:moviePlyerController.view];
        imageView.hidden = YES;
    }
}
-(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
{
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
    if ([UIImagePickerController isSourceTypeAvailable:sourceType] && [mediaTypes count] > 0) {
        NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.mediaTypes = mediaTypes;
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType =sourceType;
        [self presentModalViewController:picker animated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"设备不支持这个信息源" delegate:nil cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [alert show];
    }
}
@end
